/**
 * 
 */
package com.res.simulator.transformer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.res.common.transformer.BaseTransformer;
import com.res.simulator.handler.CommonFileHandler;
import com.res.simulator.model.RecordAndPlayModel;
import com.res.simulator.view.SearchResultView;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ReleaseVersion;
import com.sip.dbadapter.dao.domain.ScenarioRecord;
import com.sip.dbadapter.dao.domain.SystemInfo;
import com.sip.dbadapter.domain.registry.DomainRegistry;

/**
 * @author mg40327
 */
public class RecorAndPalySearchResultTransformer implements BaseTransformer<RecordAndPlayModel, List<SearchResultView>, Map<String, Object>> {

    public Map<String, Object> transformModelToDomian(final RecordAndPlayModel model) throws Exception {

        final Map<String, Object> domainMap = new HashMap<String, Object>();
        final ApiDefinition apiDefinitionDomainObj = new ApiDefinition();
        final ScenarioRecord scenarioRecordDomainObj = new ScenarioRecord();
        final ReleaseVersion releaseVersionDomainObj = new ReleaseVersion();
        final SystemInfo systemInfoDomainObj = new SystemInfo();

        apiDefinitionDomainObj.setName(model.getApiName());

        final Map<String, String> dateMap = new HashMap<String, String>();

        if (0 != model.getStartDate().trim().length())
            dateMap.put("startDate", formateDateTime(model.getStartDate()));

        if (0 != model.getEndDate().trim().length())
            dateMap.put("endDate", formateDateTime(model.getEndDate()));

        domainMap.put(DomainRegistry.DOMAIN_RELATED_GENERIC_INFO, dateMap);
        domainMap.put(DomainRegistry.API_DEFINITION, apiDefinitionDomainObj);
        domainMap.put(DomainRegistry.SCENARIO_RECORD, scenarioRecordDomainObj);
        domainMap.put(DomainRegistry.RELEASE_VERSION, releaseVersionDomainObj);
        domainMap.put(DomainRegistry.SYSTEM_INFO, systemInfoDomainObj);

        return domainMap;
    }

    public List<SearchResultView> transformDomianToView(Map<String, Object> domainMap) throws Exception {

        final List<SearchResultView> searchResultViewList = new ArrayList<SearchResultView>();

        final List<ScenarioRecord> scenarioList = (List<ScenarioRecord>) domainMap.get(DomainRegistry.SCENARIO_RECORD_LIST);
        final CommonFileHandler fileHandler = new CommonFileHandler();
        for (ScenarioRecord scenarioRecord : scenarioList) {

            final SearchResultView searchResultView = new SearchResultView();

            searchResultView.setInputValueString(scenarioRecord.getConsumeValue());
            searchResultView.setResult(scenarioRecord.getResult());
            searchResultView.setApiId(scenarioRecord.getApiDefinitionScenarioRecord().getApiId());
            searchResultView.setApiName(scenarioRecord.getApiDefinitionScenarioRecord().getName());
            searchResultView.setRecordId(scenarioRecord.getRecordId());

            final String fileName = generateFileName(scenarioRecord.getRecordId());
            final String fileUrl = fileHandler.save(scenarioRecord.getResult(), fileName);

            searchResultView.setLocation(fileUrl);

            searchResultViewList.add(searchResultView);

        }

        return searchResultViewList;
    }

    private String formateDateTime(String dateTime) throws Exception {

        //DateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss aa");
    	//change date format since ui date changed
    	DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date = (Date) dateFormatter.parse(dateTime);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        String formattedDate = formatter.format(date);

        return formattedDate;
    }

    private String generateFileName(Long recordId) throws Exception {

        final Date now = new Date();

        final String channel = "API_Search_" + recordId;

        final String fileName = channel + "-" + now.getTime() + ".xml";

        return fileName;
    }

}
